﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace SansaImageConverter
{
    /// <summary>
    /// Progress form
    /// </summary>
    public partial class ProgressForm : Form
    {
		#region Constructors (1) 

        /// <summary>
        /// Initializes a new instance of the <see cref="ProgressForm"/> class.
        /// </summary>
        public ProgressForm()
        {
            InitializeComponent();
            this.DirectoriesToConvert = new List<string>();
        }

		#endregion Constructors 

		#region Properties (2) 

        /// <summary>
        /// Gets or sets paths
        /// </summary>
        /// <value>The paths.</value>
        public List<string> DirectoriesToConvert { get; set; }

        /// <summary>
        /// Gets or sets current directory
        /// </summary>
        /// <value>The current directory.</value>
        private string CurrentDirectory { get; set; }

        /// <summary>
        /// Gets or sets current count
        /// </summary>
        /// <value>The current count.</value>
        private int CurrentCount { get; set; }

		#endregion Properties 

		#region Event Handlers (5) 

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (bwConverter.IsBusy)
            {
                if (MessageBox.Show("Are you sure you want to cancel?",
                    Application.ProductName, MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    bwConverter.CancelAsync();
                    DialogResult = DialogResult.Cancel;
                    this.Hide();
                    Application.Exit();
                }
            }
            else
            {
                DialogResult = DialogResult.OK;
                this.Hide();
                Application.Exit();
            }
        }

        /// <summary>
        /// Handles the DoWork event of the bwConverter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void bwConverter_DoWork(object sender, DoWorkEventArgs e)
        {
            XmlSettings Settings = (XmlSettings)e.Argument;

            foreach (string path in DirectoriesToConvert)
            {
                this.CurrentDirectory = path;

                string album = new DirectoryInfo(this.CurrentDirectory).Name;

                string newpath = "";
                if (Settings.CopyToPlayer)
                    newpath = Path.Combine(Path.Combine(Settings.PlayerPath, "PHOTO"), album);
                else
                    newpath = Path.Combine(Settings.ExportPath, album);

                // Creates the new path if it doesn't already exist.
                if (!Directory.Exists(newpath))
                    Directory.CreateDirectory(newpath);

                string[] files = Tools.GetImageList(path);
                this.CurrentCount = files.Length;

                for (int i = 0; i < files.Length; i++)
                {
                    string fullPath = files[i];
                    string fileName = Path.GetFileName(fullPath);
                    string newFullPath = Path.Combine(newpath, Path.GetFileNameWithoutExtension(fileName) + ".bmp");

                    bwConverter.ReportProgress(i + 1, fileName);
                    Bitmap image = Tools.ConvertImage(fullPath, Settings.RotateImages, Settings.Sizing, Settings.Quality);
                    image.Save(newFullPath, ImageFormat.Bmp);
                }
            }
        }

        /// <summary>
        /// Handles the ProgressChanged event of the bwConverter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param>
        private void bwConverter_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            lblStatus.Text = e.UserState.ToString();
            pbProgress.Value = e.ProgressPercentage;
            pbProgress.Maximum = this.CurrentCount;
            string dirName = Path.GetFileName(this.CurrentDirectory);
            this.Text = string.Format("Converting \"{0}\" - ({1}/{2})", dirName, pbProgress.Value, pbProgress.Maximum);
        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the bwConverter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        private void bwConverter_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblStatus.Text = "Conversion Done.";
            btnCancel.Text = "Close";
            Tools.Beep();
            this.Close();
        }

        /// <summary>
        /// Handles the Load event of the ProgressForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ProgressForm_Load(object sender, EventArgs e)
        {
            bwConverter.RunWorkerAsync(Program.Settings);
        }

		#endregion Event Handlers 
    }
}
